<template>
	<el-container>
		<el-header>
			<div class="left-panel">
				<el-button type="primary" icon="el-icon-plus" @click="showDialog"></el-button>
			</div>
			<div class="right-panel">
				<div class="right-panel-search">
					<el-input v-model="search.account" placeholder="登录账号" clearable></el-input>
					<el-input v-model="search.realName" placeholder="姓名" clearable></el-input>
					<el-input v-model="search.telPhone" placeholder="手机号" clearable></el-input>
					<el-button type="primary" icon="el-icon-search" @click="upsearch"></el-button>
					<el-button icon="el-icon-refresh" @click="refresh"></el-button>
				</div>
			</div>
		</el-header>
		<el-main class="nopadding">
			<scTable ref="table" :apiObj="list.apiObj" row-key="id" stripe>
				<el-table-column type="selection" width="50"></el-table-column>
				<el-table-column label="账号" prop="account" width="150"></el-table-column>
				<el-table-column label="头像" width="80" column-key="filterAvatar"
					:filters="[{ text: '已上传', value: '1' }, { text: '未上传', value: '0' }]">
					<template #default="scope">
						<el-avatar :src="scope.row.avatar" size="small"></el-avatar>
					</template>
				</el-table-column>
				<el-table-column label="姓名" prop="realName" width="150"></el-table-column>
				<el-table-column label="性别" prop="sex" width="150">
					<template #default="scope">
						<el-tag v-if="scope.row.sex == 0">保密</el-tag>
						<el-tag v-if="scope.row.sex == 1" type="success">男</el-tag>
						<el-tag v-if="scope.row.sex == 2" type="success">女</el-tag>
					</template>
				</el-table-column>
				<el-table-column label="手机号" prop="telPhone" width="250"></el-table-column>
				<el-table-column label="状态" prop="status" width="250">
					<template #default="scope">
						<el-tag v-if="scope.row.status == 2">冻结</el-tag>
						<el-tag v-if="scope.row.status == 1" type="success">正常</el-tag>
					</template>
				</el-table-column>
				<el-table-column label="注册时间" prop="createTime" width="150" sortable></el-table-column>
				<el-table-column label="最后登录时间" prop="lastLoginTime" width="150" sortable></el-table-column>
				<el-table-column label="操作" fixed="right" align="right" width="160">
					<template #default="scope">
						<el-button-group>
							<el-button text type="primary" size="small" @click="table_edit(scope.row, scope.$index)">编辑
							</el-button>
							<el-popconfirm title="确定删除吗？" @confirm="table_del(scope.row, scope.$index)">
								<template #reference>
									<el-button text type="primary" size="small">删除</el-button>
								</template>
							</el-popconfirm>
						</el-button-group>
					</template>
				</el-table-column>
			</scTable>
		</el-main>
	</el-container>
	<save-dialog v-if="dialog.save" ref="saveDialog" @success="handleSuccess" @closed="dialog.save = false">
	</save-dialog>
</template>

<script>
import saveDialog from './save'
export default {
	name: 'userList',
	components: {
		saveDialog
	},
	data() {
		return {
			dialog: {
				save: false
			},
			search: {},
			sexFilters: [
				{ text: '男', value: '男' },
				{ text: '女', value: '女' }
			],
			list: {
				apiObj: this.$API.system.user.list
			}
		}
	},
	methods: {
		showDialog() {
			this.dialog.save = true;
			this.$nextTick(() => {
				this.$refs.saveDialog.open()
			})
		},
		//编辑
		table_edit(row) {
			this.dialog.save = true
			this.$nextTick(() => {
				this.$refs.saveDialog.open('edit').setData(row)
			})
		},
		//删除
		async table_del(row, index){
				var reqData = {id: row.id}
				var res = await this.$API.system.user.del.get(reqData);
				if(res.code == 1){
					//这里选择刷新整个表格 OR 插入/编辑现有表格数据
					this.$refs.table.tableData.splice(index, 1);
					this.$message.success("删除成功")
				}else{
					this.$alert(res.message, "提示", {type: 'error'})
				}
			},
		//本地更新数据
		handleSuccess(data, mode) {
			if (mode == 'add') {
				this.$refs.table.refresh()
			} else if (mode == 'edit') {
				this.$refs.table.tableData.filter(item => item.id === data.id).forEach(item => {
					Object.assign(item, data)
				})
			}
		},
		//搜索
		upsearch() {
			this.$refs.table.upData(this.search)
		},
		//重置
		refresh() {
			this.search = {}
			this.$refs.table.reload()
		}
	}
}
</script>

<style>

</style>
